Database Design
Iquea Commerce uses MySQL 8.x as the relational database with JPA/Hibernate for ORM. The schema is automatically generated from JPA entities and populated with initial data.Database Configuration
application.properties
createDatabaseIfNotExist=true- Auto-creates databaseddl-auto=update- Creates/updates tables without data lossshow-sql=true- Logs SQL statements (disable in production)defer-datasource-initialization=true- Runs data.sql after schema creationPhysicalNamingStrategyStandardImpl- Uses exact entity table names
Database Schema
Entity-Relationship Diagram
Table Definitions
Usuario Table
Stores user accounts (customers and administrators).Email- Unique constraint enforced at database levelrol- Enum stored as VARCHAR (‘ADMIN’, ‘CLIENTE’)activo- Soft delete flag
categorias Table
Product categories (Salón, Dormitorio, Cocina, etc.).Producto Table
Furniture products with embedded value objects (Precio, Dimensiones).Precio→Cantidad+MonedacolumnsDimensiones→Alto+Ancho+Profundidadcolumns
Pedidos Table
Customer orders with auto-generated reference codes.PENDIENTE- Order created, awaiting paymentPAGADO- Payment confirmedENVIADO- Shipped to customerENTREGADO- DeliveredCANCELADO- Cancelled
A8K3N7Q2M9
Detalle_pedido Table
Order line items (products in an order).- Cascade delete - Deleting order deletes all details
- Price snapshot - Stores price at time of order (immutable)
- Quantity tracking - Number of units ordered
JPA/Hibernate Configuration
DDL Auto Modes
| Mode | Description | Use Case |
|---|---|---|
create | Drops and recreates tables on startup | Development |
create-drop | Creates on startup, drops on shutdown | Testing |
update | Updates schema without data loss | Production |
validate | Validates schema without changes | Strict production |
none | No schema management | Manual migrations |
Naming Strategy
Standard (current):@Table(name = "Usuario")→Usuariotable@Column(name = "Email")→Emailcolumn
Usuario→usuariotableemailValue→email_valuecolumn
Data Initialization
data.sql
Executed on startup after Hibernate creates the schema.INSERT IGNORE- Skips if row already exists (prevents duplicates)- Placeholder images - Using placehold.co
- Rich product data - 17 furniture items across 6 categories
- Realistic stock levels - 5-30 units per product
Database Queries
Custom Repository Queries
Method name queries:Performance Optimization
Indexing Strategy
Primary keys:- All tables have
AUTO_INCREMENTprimary keys - Clustered index for fast lookups
Usuario.Email- Fast user lookup by emailProducto.sku- Unique product identifierPedidos.referencia- Order tracking
Producto.categoria_id- Fast category filteringPedidos.usuario_id- User order historyDetalle_pedido.pedido_id- Order details lookup
Producto.es_destacado- Featured products queryPedidos.estado- Filter by order statusPedidos.fecha_pedido- Date range queries
Lazy Loading
All relationships useFetchType.LAZY to avoid N+1 query problems:
- Reduces initial query size
- Loads related entities only when accessed
- Prevents loading entire object graphs
Ensure transactions are open when accessing lazy-loaded relationships to avoid
LazyInitializationException.Database Maintenance
Backup Strategy
Full backup:View Schema
Show all tables:Common Queries
Count products by category:Entity-Table Mapping Summary
| Entity | Table | Primary Key | Relationships |
|---|---|---|---|
| Usuario | Usuario | usuario_id | → Pedidos (One-to-Many) |
| Categorias | categorias | categoria_id | → Producto (One-to-Many) |
| Producto | Producto | producto_id | ← Categorias (Many-to-One) / → Detalle_pedido (One-to-Many) |
| Pedido | Pedidos | pedido_id | ← Usuario (Many-to-One) / → Detalle_pedido (One-to-Many, cascade) |
| Detalle_pedido | Detalle_pedido | detalle_id | ← Pedido (Many-to-One) / ← Producto (Many-to-One) |
Auto Schema Generation
Hibernate creates tables from JPA entities automatically
Data Initialization
data.sql populates initial categories and products on startup
Value Objects
Embedded objects (Precio, Dimensiones) stored as columns
Referential Integrity
Foreign keys enforce relationships between tables